package com.tledu.spring06.spring04.aspect;

import org.aspectj.lang.JoinPoint;

import java.lang.reflect.Field;
import java.util.Date;

/**
 * 使用配置的方式实现aop
 *
 * @author Antg
 * @date 2021/8/914:41
 */
public class AspectWithXML {
    public void before(JoinPoint joinPoint) {
        System.out.println("xml配置方式--->拦截了");
        Object[] args = joinPoint.getArgs();
        for (Object o : args) {
            Class c = o.getClass();
            Field[] fields = c.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if ("createBy".equals(field.getName())) {
                    try {
                        field.set(o, "张三");
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
                if ("createTime".equals(field.getName())) {
                    try {
                        field.set(o, new Date());
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }
}
